package news.util;
import java.sql.*;

/**
 * @author Administrator
 * 数据库连接关闭工具类
 */
public class BaseDao {
    public Connection conn;
    public Statement st;
    public ResultSet rs;
    public PreparedStatement ps;


    /**
     * 建立sql连接
     */
    public void openDB(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/test?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC",
                    "root",
                    "root");
            System.out.println("数据库连接成功！");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 关闭sql连接
     */
    public void closeDB(){
        try {
            if (rs!=null){
                rs.close();
            }
            if (st!=null){
                st.close();
            }
            if (ps!=null){
                ps.close();
            }
            if (conn!=null){
                conn.close();
            }
            System.out.println("数据库已关闭！");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
